namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Web;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Configuration;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class TenantUriConverterFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantUriConverter>()
                .DerivesFrom<UriConverter>()
                .IsConcreteClass()
                .IsSealed()
                .NoDefaultConstructor()
                .Implements<IConvertUri>()
                .Result);
        }

        [Fact]
        public void ctor_Type_RouteName()
        {
            Assert.NotNull(new TenantUriConverter(typeof(ResourceControllerOfIResourceDummy), new RouteName("name")));
        }

        [Fact]
        public void ctor_TypeNull_RouteName()
        {
            Assert.Throws<ArgumentNullException>(() => new TenantUriConverter(null as Type, new RouteName("name")));
        }

        [Fact]
        public void ctor_Type_RouteNameNull()
        {
            Assert.Throws<ArgumentNullException>(() => new TenantUriConverter(typeof(ResourceControllerOfIResourceDummy), null as RouteName));
        }

        [Fact]
        public void prop_Urn_get_whenConneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/content/test";
                AbsoluteUri actual = new TenantUriConverter(typeof(ResourceControllerOfIResourceDummy), "content").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenLanneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/content/test";
                AbsoluteUri actual = new TenantUriConverter(typeof(ResourceControllerOfIResourceDummy), "content").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenHtml()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/content/test";
                AbsoluteUri actual = new TenantUriConverter(typeof(ResourceControllerOfIResourceDummy), "content").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenNoTenant()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>();

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/example/test.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.Null(new TenantUriConverter(typeof(ResourceControllerOfIResourceDummy), "content").Urn);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenFormConneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/form/test";
                AbsoluteUri actual = new TenantUriConverter(typeof(TenantFormController), "form").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenFormLanneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form.en").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/form/test";
                AbsoluteUri actual = new TenantUriConverter(typeof(TenantFormController), "form").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenFormHtml()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test/form.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/form/test";
                AbsoluteUri actual = new TenantUriConverter(typeof(TenantFormController), "form").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenFormNoTenant()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>();

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/example/test/form.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.Null(new TenantUriConverter(typeof(TenantFormController), "form").Urn);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenFormNestedConneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/abc/xyz/form").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/form/abc/xyz";
                AbsoluteUri actual = new TenantUriConverter(typeof(TenantFormController), "form").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenFormNestedLanneg()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/abc/xyz/form.en").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/form/abc/xyz";
                AbsoluteUri actual = new TenantUriConverter(typeof(TenantFormController), "form").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenFormNestedHtml()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/abc/xyz/form.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/form/abc/xyz";
                AbsoluteUri actual = new TenantUriConverter(typeof(TenantFormController), "form").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void prop_Urn_get_whenFormNoForm()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                AbsoluteUri expected = "urn://example.com/form/test";
                AbsoluteUri actual = new TenantUriConverter(typeof(TenantFormController), "form").Urn;

                Assert.Equal<AbsoluteUri>(expected, actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}